import { getRandomInt } from '../utils/tool.js';
import Actor from './Actor.js';

/** 石头类 */
export default class Stock extends Actor {
    constructor(ctx, x, y, width, height, radius = 30, color = 'red', moveAble = true) {
        super(ctx, x, y, width, height, 'fillCircle', radius, color);

        /** 运动步长 */
        this.stepX = getRandomInt(4, 10);
        this.stepY = getRandomInt(4, 10);

        // 是否可移动
        this.moveAble = moveAble;
    }

    update() {
        if (this.moveAble) {
            // 使自身随机运动,如果下一步到达地图边界了，则不进行运动。否则更新x的值
            let { left, right } = this.isTouchingMapWallX(this.x + this.stepX);
            let { top, bottom } = this.isTouchingMapWallY(this.y + this.stepY);

            let checkX = getRandomInt(4, 10);
            let checkY = getRandomInt(4, 10);

            if (left) this.stepX = checkX;
            if (right) this.stepX = -checkX;
            if (top) this.stepY = checkY;
            if (bottom) this.stepY = -checkY;

            this.x += this.stepX;
            this.y += this.stepY;
        }
    }

    draw() {
        super.draw();
    }
}
